/*
 * nocyfreshhelper.m
 * 
 * Copyright (c) 2009 nathulal <inathulal at gmail.com>. All rights reserved.
 * 
 * This file is part of NoCyfresh.
 * 
 * NoCyfresh is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NoCyfresh is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NoCyfresh.  If not, see <http://www.gnu.org/licenses/>.
 */

#import <Foundation/Foundation.h>
//#import <UIKit/UIKit.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>


int main(int argc, char **argv) {
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	NSData *binaryPlistData = nil;
	NSString *errorStr = nil;
	
	if(argc != 3)
		goto noaction;
	bool refreshEnabled = false;

	if(strcmp(argv[1], "-d") == 0)
	{
		refreshEnabled = false;
	}
	else if(strcmp(argv[1], "-e") == 0)
	{
		refreshEnabled = true;
	}
	
	NSString *prefsFile = [NSString stringWithUTF8String: argv[2]];
	NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:prefsFile];
	
	if (prefs != nil) {
	       [prefs autorelease];
		if(refreshEnabled)
		{
			NSDate *yesterday = [[NSDate alloc] initWithTimeIntervalSinceNow:-24 * 60 * 60];
			[prefs setObject:yesterday forKey:@"LastUpdate"];
			[yesterday release];
		}
		else
		{
			NSDate *curDate = [NSDate date];
			[prefs setObject:curDate forKey:@"LastUpdate"];
			[curDate release];
		}
		// [prefs writeToFile:prefsFile atomically:YES];
		binaryPlistData = [NSPropertyListSerialization dataFromPropertyList:prefs
                                                                 format:NSPropertyListBinaryFormat_v1_0
										errorDescription: &errorStr];

		[binaryPlistData writeToFile:prefsFile atomically:YES];
	}
noaction:
	[pool release];
	return 0;
}
